package com.zhangdi.demo.text.chinesesort;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LogReader {
    int length = 1024 * 4;
    int offset = 0;
    String key = "[coll=";

    public List<String> Reader(String file, String collName) {
        List<String> result = new ArrayList<>();
        key = key + collName;
        try (FileReader reader = new FileReader(file)) {
            BufferedReader bf = new BufferedReader(reader);
            String txt = null;
            String lastTxt = null;
            while (StringUtils.isNotBlank(txt = bf.readLine())) {
                int index = 0;
                if ((index = txt.lastIndexOf(key)) <= 0) {
                    continue;
                }
                txt = txt.substring(index);
                if (txt.equals(lastTxt)) {
                    continue;
                }
                lastTxt = txt;
                result.add(txt);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static void main(String[] args) {
//        LogReader reader = new LogReader();
//        List<String> ops = reader.Reader("D:\\workspace\\output.log", "");
//        for (String op : ops) {
//            System.out.println(op);
//        }
//        String line = "{\"coll\":\"companyCertRel\", \"op\":\"update\", \"id\":\"61e5300c9ee65db62523ede9\"}";
        String line = "[coll=companyCertRel, op=update, id=61af020047a9f75cdb784e1a]";
        line = line.replace("[", "{\"").
                replace("]", "\"}").replace("=", "\":\"").replace(", ", "\",\"");
        ObjectMapper mapper = new ObjectMapper();
        try {
            DataInfo dataInfo = mapper.readValue(line, DataInfo.class);
            System.out.println(dataInfo);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

class DataInfo {
    private String coll;
    private String op;
    private String id;

    public String getColl() {
        return coll;
    }

    public void setColl(String coll) {
        this.coll = coll;
    }

    public String getOp() {
        return op;
    }

    public void setOp(String op) {
        this.op = op;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
